package com.foodorder.service;

import com.foodorder.entity.StoreFollow;
import com.foodorder.entity.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 通知推送服务类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class NotificationService {
    
    private final StoreFollowService storeFollowService;
    private final EmailService emailService;
    private final UserService userService;
    
    /**
     * 推送店铺新品通知
     */
    public void pushNewProductNotification(Long storeId, String storeName, String productName) {
        try {
            // 获取关注该店铺且开启通知的用户
            List<StoreFollow> followers = storeFollowService.getNotificationEnabledFollowers(storeId);
            
            for (StoreFollow follow : followers) {
                User user = userService.findById(follow.getUserId());
                if (user != null && user.getEmail() != null) {
                    // 发送邮件通知
                    emailService.sendNewProductEmail(user.getEmail(), storeName, productName);
                    log.info("新品通知已发送给用户: {} ({})", user.getNickname(), user.getEmail());
                }
            }
            
            log.info("店铺 {} 新品 {} 通知推送完成，共推送 {} 个用户", storeName, productName, followers.size());
            
        } catch (Exception e) {
            log.error("推送新品通知失败: {}", e.getMessage());
        }
    }
    
    /**
     * 推送订单状态更新通知
     */
    public void pushOrderStatusNotification(String userEmail, String orderNumber, String status) {
        try {
            emailService.sendOrderStatusEmail(userEmail, orderNumber, status);
            log.info("订单状态通知已发送: {} -> {}", orderNumber, userEmail);
        } catch (Exception e) {
            log.error("推送订单状态通知失败: {}", e.getMessage());
        }
    }
    
    /**
     * 推送优惠券到期提醒
     */
    public void pushCouponExpiryNotification(String userEmail, String couponName, String expiryDate) {
        try {
            emailService.sendCouponExpiryEmail(userEmail, couponName, expiryDate);
            log.info("优惠券到期提醒已发送: {} -> {}", couponName, userEmail);
        } catch (Exception e) {
            log.error("推送优惠券到期提醒失败: {}", e.getMessage());
        }
    }
    
    /**
     * 推送注册欢迎通知
     */
    public void pushWelcomeNotification(String userEmail, String userName) {
        try {
            emailService.sendWelcomeEmail(userEmail, userName);
            log.info("欢迎邮件已发送: {}", userEmail);
        } catch (Exception e) {
            log.error("推送欢迎通知失败: {}", e.getMessage());
        }
    }
    
    /**
     * 批量推送店铺促销活动通知
     */
    public void pushPromotionNotification(Long storeId, String storeName, String promotionTitle, String promotionContent) {
        try {
            List<StoreFollow> followers = storeFollowService.getNotificationEnabledFollowers(storeId);
            
            for (StoreFollow follow : followers) {
                User user = userService.findById(follow.getUserId());
                if (user != null && user.getEmail() != null) {
                    String subject = String.format("%s - %s", storeName, promotionTitle);
                    emailService.sendSimpleEmail(user.getEmail(), subject, promotionContent);
                }
            }
            
            log.info("店铺 {} 促销活动通知推送完成，共推送 {} 个用户", storeName, followers.size());
            
        } catch (Exception e) {
            log.error("推送促销活动通知失败: {}", e.getMessage());
        }
    }
} 